import 'package:async/async.dart';
import 'package:boss/main.dart';
import 'package:boss/models/necessary_order.dart';
import 'package:boss/ui/widget/loading/loading.dart';
import 'package:flutter/cupertino.dart';
import 'package:hi_common/hi_common.dart';

class NecessaryOrderProvider extends ChangeNotifier {
  List<NecessaryOrder> necessaryOrders = [];

  List<NecessaryOrder> get useStatusNecessaryOrders {
    return necessaryOrders.where((item) => item.isUseStatus && [0, 1].contains(item.useRange)).toList();
  }

  List<NecessaryOrder> get useStatusNecessary {
    return necessaryOrders.where((item) => item.isUseStatus).toList();
  }

  Future<Result<List<NecessaryOrder>>> initData() async {
    try {
      var response = await api.queryNecessaryOrders();
      if (!response.successful()) {
        return Result.error(response.msg);
      }

      necessaryOrders = response.data;

      return Result.value(response.data);
    } catch (e) {
      return Result.error(e.toString());
    }
  }

  changeUseStatus(NecessaryOrder necessaryOrder) {
    necessaryOrder.useStatus = necessaryOrder.isUseStatus ? 1 : 0;
    notifyListeners();
  }

  updateNecessaryOrder(NecessaryOrder necessaryOrder) {
    var index = necessaryOrders.indexWhere((item) => item.necessaryId == necessaryOrder.necessaryId);
    if (index == -1) {
      return;
    }

    necessaryOrders[index] = necessaryOrder;

    notifyListeners();
  }

  addNecessaryOrder(NecessaryOrder necessaryOrder) {
    necessaryOrders.insert(0, necessaryOrder);
    notifyListeners();
  }

  removeNecessaryOrderById(int id) {
    necessaryOrders = necessaryOrders.where((item) => item.necessaryId != id).toList();
    notifyListeners();
  }

  update() async {
    try {
      showLoadingToast();
      var ids = useStatusNecessary.map((item) => item.necessaryId).toList();

      var res = await api.updateNecessaryOrdersStatus(ids);
      if (!res.successful()) {
        toast(res.msg);
        return;
      }

      toast('修改成功');
    } catch (e) {
      toast(e.toString());
    } finally {
      hideLoadingToast();
    }
  }

  addOrUpdateNecessaryOrder(NecessaryOrder data) async {
    try {
      if (data.necessaryName == null || data.necessaryName == '') {
        toast('请输入名称');
        return;
      }

      showLoadingToast();
      data.everyoneCount = data.everyoneCount ?? 1;
      data.price = data.price ?? 0;

      var res = await api.addNecessaryOrder(data);

      if (res.successful()) {
        toast(res.msg);
        return;
      }

      var isUpdate = data.necessaryId != null;

      toast(isUpdate ? '修改成功' : '添加成功');

      if (isUpdate) {
        var index = necessaryOrders.indexWhere((item) => item.necessaryId == data.necessaryId);
        if (index != -1) {
          necessaryOrders[index] = res.data;
        }
      } else {
        necessaryOrders.add(res.data);
      }
      notifyListeners();

      Navigator.pop(GlobalPageContext.instance.context);
    } catch (e) {
      toast(e.toString());
    } finally {
      hideLoadingToast();
    }
  }

  delete(int id) async {
    try {
      showLoadingToast();
      var res = await api.deleteNecessaryOrder(id);

      if (!res.successful()) {
        toast(res.msg);
        return;
      }
      toast('删除成功');

      necessaryOrders.removeWhere((item) => item.necessaryId == id);
      notifyListeners();

      Navigator.of(GlobalPageContext.instance.context).pop();
    } catch (e) {
      toast(e.toString());
    } finally {
      hideLoadingToast();
    }
  }
}
